package com.walter.learn.UseThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ProjectName: learnOwner
 * @Package: com.walter.learn.UseThread
 * @ClassName: SingleThreadPollDemo
 * @Description: 单线程使用
 * @Author: 唐朝
 * @CreateDate: 2019/2/19 9:32
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/2/19 9:32
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SingleThreadPollDemo {
    public static void main(String[] args) {
        // 创建一个使用单个 worker 线程的 Executor，以无界队列方式来运行该线程。
        ExecutorService pool = Executors.newSingleThreadExecutor();

        Runnable task1 = new SingelTask();
        Runnable task2 = new SingelTask();
        Runnable task3 = new SingelTask();

        pool.execute(task3);
        pool.execute(task2);
        pool.execute(task1);

        // 等待已提交的任务全部结束 不再接受新的任务
        pool.shutdown();
    }
}

class SingelTask implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行… …");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "执行完毕");
    }


}
